package com.shfl.servermanage.logic.api;


import com.github.pagehelper.PageInfo;
import com.shfl.servermanage.common.*;
import com.shfl.servermanage.logic.constants.CommonConstants;
import com.shfl.servermanage.logic.model.request.AddSiteRequest;
import com.shfl.servermanage.logic.model.request.RemoveByIdRequest;
import com.shfl.servermanage.logic.model.response.ServerSitePageResponse;
import com.shfl.servermanage.logic.model.response.SiteDetailResponse;
import com.shfl.servermanage.logic.model.response.SysServerPageResponse;
import com.shfl.servermanage.logic.service.SysServerSiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/server/site")
@Api(value = "站点", description = "站点")
public class SysServerSiteController {


    @Autowired
    private SysServerSiteService sysServerSiteService;

    @GetMapping(value = "/page/list")
    public ApiResultPagingHelper<ServerSitePageResponse, Void> page(
            RequestHelper requestHelper
    ) {
        requestHelper.apply();
        Map<String, Object> objectMap = requestHelper.getKeyMap();
        PageInfo<ServerSitePageResponse> pageInfo = sysServerSiteService.findPage(requestHelper.getPageable(), objectMap);
        if (pageInfo.getTotal() > 0) {
            pageInfo.getList().forEach(item -> {
//                item.setJobStatusValue(CommonConstants.JobStatus.getName(item.getJobStatus()));
//                item.setJobTypeValue(CommonConstants.JobType.getName(item.getJobType()));
            });
        }
        return new ApiResultPagingHelper<>(SystemConstant.RE_SUCCESS_CODE,
                pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum(),
                pageInfo.getList());
    }


    @GetMapping(value = "/list")
    public ApiResultListHelper<ServerSitePageResponse> list(
            RequestHelper requestHelper
    ) {
        requestHelper.apply();
        Map<String, Object> objectMap = requestHelper.getKeyMap();
        objectMap.put("deleteFlg", CommonConstants.DeleteFlg.NOT_DELETE.getCode());
        List<ServerSitePageResponse> list = sysServerSiteService.findList(objectMap);
        return new ApiResultListHelper<>(SystemConstant.RE_SUCCESS_CODE, list);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增", notes = "新增")
    public ApiResultHelper add(
            @RequestBody AddSiteRequest addSiteRequest
    ) throws Exception {
        addSiteRequest.setSiteDirectory(CommonConstants.BT_ROOT_DIRECTORY.concat("/" + addSiteRequest.getSiteDomain()));
        sysServerSiteService.add(addSiteRequest);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }

    @PostMapping(value = "/remove")
    @ApiOperation(value = "移除", notes = "移除")
    public ApiResultHelper remove(
            @RequestBody RemoveByIdRequest removeByIdRequest
    ) throws Exception {
        sysServerSiteService.remove(removeByIdRequest);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }

    @GetMapping("detail")
    @ApiOperation(value = "详情", notes = "详情")
    public ApiResultSimpleHelper<SiteDetailResponse> detail(
            @ApiParam(name = "id", example = "1", value = "站点Id", required = true)
            @RequestParam(value = "id") Long id
    ) {

        SiteDetailResponse siteDetailResponse = new SiteDetailResponse();
        sysServerSiteService.detail(siteDetailResponse, id);
        return new ApiResultSimpleHelper<>(SystemConstant.RE_SUCCESS_CODE, siteDetailResponse);
    }
}
